package com.wk.search.listener;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.wk.common.constant.mq.ItemExchange;
import com.wk.common.constant.mq.ItemQueue;
import com.wk.common.constant.mq.ItemRoutingKey;
import com.wk.pms.entity.*;
import com.wk.pms.feign.*;
import com.wk.search.entity.Goods;
import com.wk.search.entity.SearchAttr;
import com.wk.search.repository.GoodsRepository;
import com.wk.wms.entity.WareSku;
import com.wk.wms.feign.WareSkuFeign;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.ZoneOffset;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 监听商品的增改删, 并对ES中的数据做对应处理
 */
@Component
public class GoodsListener {

    @Autowired
    private BrandFeign brandFeign;

    @Autowired
    private CategoryFeign categoryFeign;

    @Autowired
    private ProductAttrValueFeign attrValueFeign;

    @Autowired
    private SkuInfoFeign skuInfoFeign;

    @Autowired
    private SpuInfoFeign spuInfoFeign;

    @Autowired
    private WareSkuFeign wareSkuFeign;

    @Autowired
    private GoodsRepository goodsRepository;

    @RabbitListener(bindings = @QueueBinding(   //声明绑定信息
        exchange = @Exchange(value = ItemExchange.PMS_EXCHANGE,ignoreDeclarationExceptions = "true"),   //声明交换机, 交换机类型,忽略声明异常
        value = @Queue(value = ItemQueue.SEARCH_QUEUE,durable = "true"), //声明routingKey对应的队列
        key = {ItemRoutingKey.ITEM_INSERT}    //声明路由键 监听增改操作的routingKey
    ))
    public void listener(Long spuId){
        List<SkuInfo> skus = skuInfoFeign.queryBySpuId(spuId).getData();
        SpuInfo spu = spuInfoFeign.querySpuById(spuId).getData();
        if(CollectionUtils.isNotEmpty(skus)){
            //sku转goods对象
            List<Goods> goodsList = skus.stream().map(sku -> {
                Goods goods = new Goods();
                goods.setSkuId(sku.getSkuId());
                Date date = Date.from(spu.getCreateTime().atZone(ZoneOffset.ofHours(8)).toInstant());
                goods.setCreateTime(date);
                goods.setPic(sku.getSkuDefaultImg());
                goods.setPrice(sku.getPrice().doubleValue());
                goods.setSale(0L);
                goods.setTitle(sku.getSkuTitle());

                //搜索属性
                List<ProductAttrValue> valueEntities = attrValueFeign.querySearchAttrValueBySpuId(spuId).getData();
                if(CollectionUtils.isNotEmpty(valueEntities)){
                    List<SearchAttr> searchAttrList = valueEntities.stream().map(value -> {
                        SearchAttr searchAttr = new SearchAttr();
                        searchAttr.setAttrId(value.getAttrId());
                        searchAttr.setAttrName(value.getAttrName());
                        searchAttr.setAttrValue(value.getAttrValue());
                        return searchAttr;
                    }).collect(Collectors.toList());
                    goods.setAttrs(searchAttrList);
                }

                //品牌数据
                goods.setBrandId(sku.getBrandId());
                Brand brand = brandFeign.info(sku.getBrandId()).getData();
                if (brand != null) {
                    goods.setBrandName(brand.getName());
                }

                //分类数据
                goods.setCategoryId(sku.getCatalogId());
                Category category = categoryFeign.info(sku.getCatalogId()).getData();
                if (category != null) {
                    goods.setCategoryName(category.getName());
                }

                //库存数据
                List<WareSku> wareSkuList = wareSkuFeign.queryBySkuId(sku.getSkuId()).getData();
                if(CollectionUtils.isNotEmpty(wareSkuList)){
                    //anyMatch: 只要有一个匹配就返回true
                    boolean flag = wareSkuList.stream().anyMatch(waresku -> waresku.getStock() > 0);
                    goods.setStore(flag);
                }

                return goods;
            }).collect(Collectors.toList());

            //导入索引库
            goodsRepository.saveAll(goodsList);
        }
    }

}
